%{
#include <stdio.h>
#include "token.h"
int yylex();
void yyerror(char * s);
#include "symtab.h"
%}
%union {int number;
	tokentype token;
        }

%token PROG PERIOD WRITELN BEG END ASG DIV EXP
%token <token> ID NUM

%start program
%left '+' '-' 
%left '*' DIV
%left EXP

%%
program : PROG ID ';' block PERIOD {addname($2.str);}
	;
block	: BEG stmtlist END
	;
stmtlist : stmtlist ';' stmt
	| stmt
        | error { yyerror("illegal statement \n");}
	;
stmt :    astmt 
        | writestmt 
        | error { yyerror("What's up \n"); }

  
	;
writestmt: WRITELN '(' exp ')'
	;
astmt : ID ASG exp              {addname($1.str);}
	;
exp	: exp '+' exp		{addop("+");}
        | exp '-' exp		{addop("-");}
	| exp '*' exp		{addop("*");}
	| exp DIV exp		{addop("div");}
	| exp EXP exp		{addop("exp");}
        | '(' exp ')'
	| ID			{addname($1.str);}
        | constant
	| error { yyerror("illegal expression\n");}  
	;
constant: NUM                   {}

%%

void yyerror(char* s) {
        fprintf(stderr,"%s\n",s);
        }

int
main() {
    printf("1\t");
    yyparse();
    printnames();
    printops();
    return 1;
    }
